// miniprogram/pages/my/activity/index.js
import Dialog from '@vant/weapp/dialog/dialog';
import {UserModel} from '../../../model/user'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    list: [],
    list2: [],
    list3: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 报名
    this.loadUserPartyActivity1(1)
    //  完成
    this.loadUserPartyActivity2(2)
    // 取消
    this.loadUserPartyActivity3(-1)
  },
  loadUserPartyActivity1(stateCode){
    UserModel.userPartyList(stateCode).then((res) => {
      console.log(res)
      if(res && res.code == 200) {
        this.setData({
          list:res.data
        })
      }else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  loadUserPartyActivity2(stateCode){
    UserModel.userPartyList(stateCode).then((res) => {
      console.log(res)
      if(res && res.code == 200) {
        this.setData({
          list2:res.data
        })
      }else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  loadUserPartyActivity3(stateCode){
    UserModel.userPartyList(stateCode).then((res) => {
      console.log(res)
      if(res && res.code == 200) {
        this.setData({
          list3:res.data
        })
      }else {
        console.log(res)
      }
    }).catch(e => {
      console.log(e)
    })
  },
  typeTap(e) {
    let type= e.currentTarget.dataset.type
    console.log('type',type)
    this.setData({
      type: type
    })
    if(type == 1){
      this.loadUserPartyActivity1(1)
      
    } else if(type == 2){
      this.loadUserPartyActivity3(-1)
    } else {
      this.loadUserPartyActivity2(2)
    }
  }, 
  // 取消活动 / 心愿
  cancel(e) {
    console.log(e)
    let activityPartyId = e.currentTarget.dataset.id

    Dialog.confirm({
      message: `您将取消已报名，是否确认取消?`,
    })
      .then(() => {
        // on confirm todo 根据传入类型 调用取消接口
        console.log('确认')
        let data = {
          stateCode:-1,
          activityId:activityPartyId
        }
        UserModel.userCancelParty(data).then((res) => {
          console.log(res)
          if(res && res.code == 200) {
            this.setData({
              list2:res.rows
            })
            this.loadUserPartyActivity1(1)
          }else {
            console.log(res)
          }
        }).catch(e => {
          console.log(e)
        })
      })
      .catch(() => {
        // on cancel
        console.log('取消')
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadUserPartyActivity1(1)
    this.loadUserPartyActivity2(2)
    this.loadUserPartyActivity3(-1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})